<?php
namespace Tongcheng\Controller;
use Think\Controller;
class ShopCateController extends CommonController {
    public function index(){
    	$model = M("Cate");
    	$map['wid'] = session('wid');
        $map['pid'] = 0;
        $map['type'] = "shop";
    	$list = $model->where($map)->select();
        unset($map['type']);
        foreach ($list as $key => $value) {
            $map['pid'] = $value['id'];
            $list[$key]['sub_cate'] = $model->where($map)->select();
        }
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){
        if(IS_AJAX){
            $model = M("Cate");
            $new['wid'] = session('wid');
            $new['pid'] = 0;
            $new['type'] = 'shop';
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            if($model->add($new)){
                $this->success('修改成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
    	$model = M("Cate");
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function sub_add(){
        $model = M("Cate");

        if(IS_AJAX){
            $new['wid'] = session('wid');
            $new['pid'] = $_GET['id'];
            $new['type'] = 'shop';
            $new['title'] = I('post.title');
            $new['url'] = '';
            $new['imgurl'] = I('post.imgurl');
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display('add');
        }
    }

    public function sub_edit(){
        $model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
    	$model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}